﻿using System;
using System.ComponentModel.DataAnnotations;


namespace CoreLims.Models.CoreLims
{
    public sealed partial class Reagent
    {
        public partial class Reagents
        {
            public Reagents()
            {
                this.Id = Guid.NewGuid().ToString();
                this.IsEnable = true;
            }
            public string Id { get; set; }

            [Required(ErrorMessage = "名称必须填写")]
            /// <summary>
            /// 试剂名称
            /// </summary>
            public string Name { get; set; }

            /// <summary>
            /// 试剂编号
            /// </summary>
            public string Number { get; set; }

            [Required(ErrorMessage = "类型必须填写")]
            /// <summary>
            /// 试剂类型
            /// </summary>
            public string Type { get; set; }

            /// <summary>
            /// 有效时长
            /// </summary>
            public int Validity { get; set; }

            /// <summary>
            /// 有效时长单位
            /// </summary>
            public string ValidityUnit { get; set; }

            /// <summary>
            /// 到期提醒天数
            /// </summary>
            public int? Remind { get; set; }

            /// <summary>
            /// 物料代码 如果关联物料代码，该试剂配制完成后会自动进入库存
            /// </summary>
            public string MaterialCode { get; set; }

            /// <summary>
            /// 物料名称
            /// </summary>
            public string MaterialName { get; set; }

            /// <summary>
            /// 依据标准
            /// </summary>
            public string AdoptionStandard { get; set; }

            /// <summary>
            /// 浓度
            /// </summary>
            public string Concentration { get; set; }

            /// <summary>
            /// 默认配制数量
            /// </summary>
            public decimal? PreparationQuantity { get; set; }

            /// <summary>
            /// 数量单位
            /// </summary>
            public string QuantityUnit { get; set; }

            /// <summary>
            /// 配制方法
            /// </summary>
            public string PreparationMethod { get; set; }

            /// <summary>
            /// 是否启用  当依据标准或配制方法更新时时，禁用旧的启用新的，防止关联信息丢失。禁用的试剂不允许再配制。
            /// </summary>
            public bool IsEnable { get; set; }

            /// <summary>
            /// 提醒人ID
            /// </summary>
            public string RemindUserId { get; set; }

            /// <summary>
            /// 提醒人姓名
            /// </summary>
            public string RemindUserName { get; set; }

            /// <summary>
            /// 提醒组ID
            /// </summary>
            public string RemindGroupId { get; set; }

            /// <summary>
            /// 提醒组名
            /// </summary>
            public string RemindGroupName { get; set; }

            public Reagents Copy(Reagents other)
            {
                var copy = new Reagents();
                copy.Name = other.Name;
                copy.Number = other.Number;
                copy.Type = other.Type;
                copy.Validity = other.Validity;
                copy.ValidityUnit = other.ValidityUnit;
                copy.Remind = other.Remind;
                copy.MaterialCode = other.MaterialCode;
                copy.MaterialName = other.MaterialName;
                copy.AdoptionStandard = other.AdoptionStandard;
                copy.Concentration = other.Concentration;
                copy.PreparationQuantity = other.PreparationQuantity;
                copy.QuantityUnit = other.QuantityUnit;
                copy.PreparationMethod = other.PreparationMethod;
                copy.RemindUserId = other.RemindUserId;
                copy.RemindUserName = other.RemindUserName;
                copy.RemindGroupId = other.RemindGroupId;
                copy.RemindGroupName = other.RemindGroupName;
                return copy;
            }
        }

        public partial class Component
        {
            public Component()
            {
                this.Id = Guid.NewGuid().ToString();
                this.IsInventory = false;
            }
            public string Id { get; set; }

            /// <summary>
            /// 该组分为配制的试剂ID
            /// </summary>
            public string ReagentId { get; set; }

            /// <summary>
            /// 组分名称
            /// </summary>
            public string Name { get; set; }

            /// <summary>
            /// 物料代码
            /// </summary>
            public string MaterialCode { get; set; }

            /// <summary>
            /// 物料名称
            /// </summary>
            public string MaterialName { get; set; }

            /// <summary>
            /// 浓度
            /// </summary>
            public string Concentration { get; set; }

            /// <summary>
            /// 该组分的消耗量
            /// </summary>
            public decimal? Dosage { get; set; }

            /// <summary>
            /// 消耗量单位
            /// </summary>
            public string DosageUnit { get; set; }

            /// <summary>
            /// 是否消耗库存 设置为‘true’则说明使用该组分时会消耗对应的物料
            /// </summary>
            public bool IsInventory { get; set; }

            /// <summary>
            /// 配制方法
            /// </summary>
            public string PreparationMethod { get; set; }

            /// <summary>
            /// 更新，从添加组份界面更新组份时使用该函数
            /// </summary>
            /// <param name="other"></param>
            public void Copy(Component other)
            {
                this.Name = other.Name;
                this.MaterialCode = other.MaterialCode;
                this.MaterialName = other.MaterialName;
                this.Concentration = other.Concentration;
                this.Dosage = other.Dosage;
                this.DosageUnit = other.DosageUnit;
                this.IsInventory = other.IsInventory;
                this.PreparationMethod = other.PreparationMethod;
            }
        }

        public partial class Record
        {
            public Record()
            {
                this.Id = Guid.NewGuid().ToString();
                this.PreparationTime = DateTime.Now;
            }
            public string Id { get; set; }

            /// <summary>
            /// 该配制记录所关联的试剂ID
            /// </summary>
            public string ReagentId { get; set; }

            /// <summary>
            /// 名称 由关联的试剂带过来
            /// </summary>
            public string Name { get; set; }

            /// <summary>
            /// 编号 由关联的试剂带过来
            /// </summary>
            public string Number { get; set; }

            /// <summary>
            /// 类型 由关联的试剂带过来
            /// </summary>
            public string Type { get; set; }

            /// <summary>
            /// 有效期至 由关联的试剂有效时长和配制日期计算得来
            /// </summary>
            public DateTime? ValidityTo { get; set; }

            /// <summary>
            /// 环境温度
            /// </summary>
            public decimal? Temperature { get; set; }

            /// <summary>
            /// 温度单位
            /// </summary>
            public string TemperatureUnit { get; set; }

            /// <summary>
            /// 环境湿度
            /// </summary>
            public decimal? Humidity { get; set; }

            /// <summary>
            /// 湿度单位
            /// </summary>
            public string HumidityUnit { get; set; }

            /// <summary>
            /// 配制时间
            /// </summary>
            public DateTime PreparationTime { get; set; }

            /// <summary>
            /// 配制人ID
            /// </summary>
            public string PreparationById { get; set; }

            /// <summary>
            /// 配制人姓名
            /// </summary>
            public string PreparationByName { get; set; }

            /// <summary>
            /// 浓度 默认值由关联的试剂带过来
            /// </summary>
            public string Concentration { get; set; }

            /// <summary>
            /// 配制数量
            /// </summary>
            public decimal? PreparationQuantity { get; set; }

            /// <summary>
            /// 配制数量单位
            /// </summary>
            public string QuantityUnit { get; set; }

            /// <summary>
            /// 物料代码 由关联的试剂带过来，有物料代码则自动入库。
            /// </summary>
            public string MaterialCode { get; set; }

            /// <summary>
            /// 物料名称
            /// </summary>
            public string MaterialName { get; set; }

            /// <summary>
            /// 试剂关联的“入库单”ID。
            /// 配制试剂将自动生成对应物料的“入库单”，系统根据入库单再自动生成出库单，这样试剂配制好系统即可自动领用。
            /// </summary>
            public string InventoryId { get; set; }

            /// <summary>
            /// 有效期到期前提醒天数 由关联的试剂带过来
            /// </summary>
            public int? ValidityRemind { get; set; }

            /// <summary>
            /// 提醒人ID
            /// </summary>
            public string RemindUserId { get; set; }

            /// <summary>
            /// 提醒组ID
            /// </summary>
            public string RemindGroupId { get; set; }

            /// <summary>
            /// 记录状态  需要审核的配制记录配制时首先会锁定，锁定完成恢复正常状态后才能进入库存进行领用。
            /// </summary>
            public string Status { get; set; }

            /// <summary>
            /// 根据界面传过来的配制信息和对应的试剂定义，生成配制记录
            /// </summary>
            /// <param name="other"></param>
            /// <returns></returns>
            public static Record Copy(Record other, Reagents reagents)
            {
                Record copy = new Record();
                copy.Name = other.Name;
                copy.ReagentId = other.ReagentId;
                copy.Concentration = other.Concentration;
                copy.PreparationQuantity = other.PreparationQuantity;
                copy.QuantityUnit = other.QuantityUnit;
                copy.Temperature = other.Temperature;
                copy.TemperatureUnit = other.TemperatureUnit;
                copy.Humidity = other.Humidity;
                copy.HumidityUnit = other.HumidityUnit;
                copy.Number = reagents.Number;
                copy.Type = reagents.Type;
                copy.MaterialCode = reagents.MaterialCode;
                copy.MaterialName = reagents.MaterialName;
                copy.ValidityRemind = reagents.Remind;
                copy.RemindUserId = reagents.RemindUserId;
                copy.RemindGroupId = reagents.RemindGroupId;
                copy.Status = "正常";
                CreateValidityTo(reagents, copy);

                return copy;
            }

            /// <summary>
            /// 生成有效期
            /// </summary>
            /// <param name="reagents"></param>
            /// <param name="copy"></param>
            private static void CreateValidityTo(Reagents reagents, Record copy)
            {
                if (reagents.ValidityUnit == "天")
                    copy.ValidityTo = copy.PreparationTime.AddDays(reagents.Validity);
                if (reagents.ValidityUnit == "周")
                    copy.ValidityTo = copy.PreparationTime.AddDays(reagents.Validity * 7);
                if (reagents.ValidityUnit == "月")
                    copy.ValidityTo = copy.PreparationTime.AddMonths(reagents.Validity);
                if (reagents.ValidityUnit == "年")
                    copy.ValidityTo = copy.PreparationTime.AddYears(reagents.Validity);
            }

        }

    }
}
